//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "VanillaSwingOption.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instruments/StrikedTypePayoff.h>
#include <gen/QL/Instruments/SwingExercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/OneAssetOption.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CVanillaSwingOption::CVanillaSwingOption (Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Cephei::QL::Instruments::ISwingExercise^ ex, UInt64 minExerciseRights, UInt64 maxExerciseRights, Cephei::QL::IPricingEngine^ QL_Pricer) : COneAssetOption(CVanillaSwingOption::typeid)
{
    CStrikedTypePayoff^ _Cpayoff;
    CSwingExercise^ _Cex;
    try
    {
#ifdef HANDLE
        _phVanillaSwingOption = NULL;
#endif
        _Cpayoff = safe_cast<CStrikedTypePayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::StrikedTypePayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::StrikedTypePayoff>&> (_Cpayoff->GetShared ()); 
        _Cex = safe_cast<CSwingExercise^> (ex);
        _Cex->Lock();
        boost::shared_ptr<QuantLib::SwingExercise>& _ex = static_cast<boost::shared_ptr<QuantLib::SwingExercise>&> (_Cex->GetShared ()); 
        QuantLib::Size _minExerciseRights = (QuantLib::Size)ValueHelper::Convert (minExerciseRights); //d
        QuantLib::Size _maxExerciseRights = (QuantLib::Size)ValueHelper::Convert (maxExerciseRights); //d
        _ppVanillaSwingOption = new boost::shared_ptr<QuantLib::VanillaSwingOption> (new QuantLib::VanillaSwingOption ( _payoff,  _ex,  _minExerciseRights,  _maxExerciseRights ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppVanillaSwingOption)->setPricingEngine (_QL_Pricer);
        SetOneAssetOption (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaSwingOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (_Cex != nullptr) _Cex->Unlock();
    }
}
Cephei::QL::Instruments::CVanillaSwingOption::CVanillaSwingOption (boost::shared_ptr<QuantLib::VanillaSwingOption>& childNative, Object^ owner) : COneAssetOption(CVanillaSwingOption::typeid)
{
#ifdef HANDLE
	_phVanillaSwingOption = NULL;
#endif
	_ppVanillaSwingOption = &childNative;
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaSwingOption));
}
Cephei::QL::Instruments::CVanillaSwingOption::CVanillaSwingOption (QuantLib::VanillaSwingOption& childNative, Object^ owner) : COneAssetOption(CVanillaSwingOption::typeid)
{
#ifdef HANDLE
	_phVanillaSwingOption = NULL;
#endif
	_ppVanillaSwingOption = new boost::shared_ptr<QuantLib::VanillaSwingOption> (&childNative);
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaSwingOption));
    _VanillaSwingOptionOwner = owner;
    _OneAssetOptionOwner = owner;
}

Cephei::QL::Instruments::CVanillaSwingOption::CVanillaSwingOption (CVanillaSwingOption^ copy) : COneAssetOption(CVanillaSwingOption::typeid)
{
#ifdef HANDLE
	_phVanillaSwingOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVanillaSwingOption = new boost::shared_ptr<QuantLib::VanillaSwingOption> (copy->GetShared());
        _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaSwingOption));
    }
}
Cephei::QL::Instruments::CVanillaSwingOption::CVanillaSwingOption (PLATFORM::Type^ t) : COneAssetOption(CVanillaSwingOption::typeid)
{
#ifdef HANDLE
	_phVanillaSwingOption = NULL;
#endif
	if (!t->IsSubclassOf(CVanillaSwingOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CVanillaSwingOption::CVanillaSwingOption (QuantLib::Handle<QuantLib::VanillaSwingOption>& childNative, Object^ owner)  : COneAssetOption(CVanillaSwingOption::typeid)
{
	_phVanillaSwingOption = &childNative;
	_ppVanillaSwingOption = &static_cast<boost::shared_ptr<QuantLib::VanillaSwingOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaSwingOption));
    _VanillaSwingOptionOwner = owner;
}
Cephei::QL::Instruments::CVanillaSwingOption::CVanillaSwingOption (QuantLib::Handle<QuantLib::VanillaSwingOption> childNative)  : COneAssetOption(CVanillaSwingOption::typeid)
{
	_phVanillaSwingOption = &childNative;
	_ppVanillaSwingOption = &static_cast<boost::shared_ptr<QuantLib::VanillaSwingOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaSwingOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CVanillaSwingOption::CVanillaSwingOption (QuantLib::VanillaSwingOption childNative)  : COneAssetOption(CVanillaSwingOption::typeid)
{
#ifdef HANDLE
	_phVanillaSwingOption = NULL;
#endif
	_ppVanillaSwingOption = new boost::shared_ptr<QuantLib::VanillaSwingOption> (new QuantLib::VanillaSwingOption (childNative));
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaSwingOption));
}
#endif

Cephei::QL::Instruments::CVanillaSwingOption::~CVanillaSwingOption ()
{
    if (_ppVanillaSwingOption != NULL)
    {
	    delete _ppVanillaSwingOption;
        _ppVanillaSwingOption = NULL;
    }
}
Cephei::QL::Instruments::CVanillaSwingOption::!CVanillaSwingOption ()
{
    if (_ppVanillaSwingOption != NULL)
    {
	    delete _ppVanillaSwingOption;
    }
}
QuantLib::VanillaSwingOption& Cephei::QL::Instruments::CVanillaSwingOption::GetReference ()
{
    if (_ppVanillaSwingOption == NULL) throw REFNEW NativeNullException ();
	return **_ppVanillaSwingOption;
}
boost::shared_ptr<QuantLib::VanillaSwingOption>& Cephei::QL::Instruments::CVanillaSwingOption::GetShared ()
{
    if (_ppVanillaSwingOption == NULL) throw REFNEW NativeNullException ();
	return *_ppVanillaSwingOption;
}
QuantLib::VanillaSwingOption* Cephei::QL::Instruments::CVanillaSwingOption::GetPointer ()
{
    if (_ppVanillaSwingOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppVanillaSwingOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VanillaSwingOption>& Cephei::QL::Instruments::CVanillaSwingOption::GetHandle ()
{
	if (_phVanillaSwingOption == NULL)
	{
		_phVanillaSwingOption = new Handle<QuantLib::VanillaSwingOption> (*_ppVanillaSwingOption);
	}
	return *_phVanillaSwingOption;
}
#endif
bool Cephei::QL::Instruments::CVanillaSwingOption::HasNative () 
{
	return (_ppVanillaSwingOption != NULL);
}

Boolean Cephei::QL::Instruments::CVanillaSwingOption::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppVanillaSwingOption)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IVanillaSwingOption^ Cephei::QL::Instruments::CVanillaSwingOption_Factory::Create (Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Cephei::QL::Instruments::ISwingExercise^ ex, UInt64 minExerciseRights, UInt64 maxExerciseRights, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CVanillaSwingOption ( payoff,  ex,  minExerciseRights,  maxExerciseRights,  QL_Pricer);
}
